1.2 Assume that the classes below are all in the file Polymorphism.java. Determine the output when the project is run. Would the output be different if the call to println were System.out.println (a.toString())? import java.util.*; public class Polymorphism { public static void main (String args [ ]) { new Polymorphism().run(); } // method main public void run() { Scanner sc = new Scanner (System.in)); A a; int code = sc.nextInt(); if (code == 0) a = new A(); else // non-zero int entered a = new D(); System.out.println (a); } // method run } // class Polymorphism class A { public String toString () { return "A"; } // method toString } // class A class D extends A { public String toString () { return "D"; } // method toString } // class D | |
| View Solution | |
| << Back | Next >> |